package com.sihan.swvp.service.video;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sihan.swvp.common.Mp4Util;
import com.sihan.swvp.dao.video.VideoManagerDao;
import com.sihan.swvp.entity.video.VideoSyn;

/**
 * 视频管理Service
 * @author jch
 *
 */
@Service
public class VideoManagerService {
	
	@Autowired
	private VideoManagerDao dao;

	/**
	 * 获取视频
	 * @param limit 每页数量
	 * @param page 页数
	 * @param params 参数
	 * @return
	 */
	public Page<Map<String, Object>> getVideo(int page,int limit,Map<String,Object> params) {
		PageHelper.startPage(page,limit);

		return dao.getVideo(params);
	}
	
	/**
	 * 通过视频ID获取视频数据
	 * @param videoid 视频ID
	 * @return
	 */
	public VideoSyn getVideoById(int videoid){
		
		return dao.getVideoById(videoid);
	}
	
	public byte[] getVideoPic(int videoid,double time){
		VideoSyn v = getVideoById(videoid);
		
		byte[] bytes= Mp4Util.getVideoPic(v.getLocpath(), time);
		
		return bytes;
	}
	
	public byte[] getVideoPic(int videoid,double time,int width,int height){
		VideoSyn v = getVideoById(videoid);
		
		byte[] bytes= Mp4Util.getVideoPic(v.getRawpath(), time, width, height);
		
		return bytes;
	}
	
	
	public List<Map<String,Object>> getWgbj(Integer videoid){
		
		return dao.getWgbj(videoid);
	}

	public void deleteVideo(VideoSyn v) throws Exception {
		// TODO Auto-generated method stub
		try {
			dao.deleteVideo(v.getId());
			Mp4Util.delete(v.getLocpath());
		} catch (Exception e) {
			throw new Exception("删除视频文件失败，请稍后尝试。");
		}
	}
}
